package com.link2room.rodimus.controller.manager;

import com.link2room.rodimus.facade.IReportFacadeService;
import com.link2room.sideswipe.permissionResouces.annotation.L2RPermissionResources;
import com.link2room.rodimus.dto.param.manager.FansReportManagerParamDto;
import com.link2room.rodimus.dto.param.manager.MallOrderManagerParamDto;
import com.link2room.rodimus.dto.param.manager.PaymentReportManagerParamDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import static com.link2room.rodimus.constant.PermissionConstant.*;

/**
 * Created by VegetaKo on 2016/12/16.
 */
@Api(tags = "报表相关")
@RestController
@RequestMapping("/manager/Reports")
public class ReprotManagerController {

    @Resource
    private IReportFacadeService reportFacadeService;

    @ApiOperation(value = "分销员粉丝报表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = false)
    })
    @RequestMapping(value = "FansReportBySalesman", method = RequestMethod.GET)
    @L2RPermissionResources(value = fansReportBySalesman, name = "分销员粉丝报表")
    public void getFansReportBySalesman(
            HttpServletResponse response,
            @RequestParam(name = "startDate", required = false) String startDate
    ) throws Exception {
        FansReportManagerParamDto fansReportManagerParamDto = new FansReportManagerParamDto();
        fansReportManagerParamDto.setStartDatetime( startDate);

        String OUTPUT = reportFacadeService.getFansReportBySalesman(fansReportManagerParamDto);

        outfile(response, OUTPUT);

    }

    @ApiOperation(value = "酒店粉丝报表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = false)
    })
    @RequestMapping(value = "FansReportByHotel", method = RequestMethod.GET)
    @L2RPermissionResources(value = fansReportByHotel, name = "酒店粉丝报表")
    public void getFansReportByHotel(
            HttpServletResponse response,
            @RequestParam(name = "startDate", required = false) String startDate
    ) throws Exception {
        FansReportManagerParamDto fansReportManagerParamDto = new FansReportManagerParamDto();
        fansReportManagerParamDto.setStartDatetime( startDate);

        String OUTPUT = reportFacadeService.getFansReportByHotel(fansReportManagerParamDto);

        outfile(response, OUTPUT);

    }

    @ApiOperation(value = "分销员分销奖励报表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = false)
    })
    @RequestMapping(value = "PaymentReportBySalesman", method = RequestMethod.GET)
    @L2RPermissionResources(value = paymentReportBySalesman, name = "分销员分销奖励报表")
    public void getPaymentReportBySalesman(
            HttpServletResponse response,
            @RequestParam(name = "startDate", required = false) String startDate
    ) throws Exception {
        PaymentReportManagerParamDto paymentReportManagerParamDto = new PaymentReportManagerParamDto();
        paymentReportManagerParamDto.setStartDatetime( startDate);
        String OUTPUT = reportFacadeService.getPaymentReportBySalesman( paymentReportManagerParamDto);
        outfile(response, OUTPUT);

    }

    @ApiOperation(value = "酒店分销奖励报表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = false)
    })
    @RequestMapping(value = "PaymentReportByHotel", method = RequestMethod.GET)
    @L2RPermissionResources(value = paymentReportByHotel, name = "酒店分销奖励报表")
    public void getPaymentReportByHotel(
            HttpServletResponse response,
            @RequestParam(name = "startDate", required = false) String startDate
    ) throws Exception {
        PaymentReportManagerParamDto paymentReportManagerParamDto = new PaymentReportManagerParamDto();
        paymentReportManagerParamDto.setStartDatetime( startDate);
        String OUTPUT = reportFacadeService.getPaymentReportByHotel( paymentReportManagerParamDto);
        outfile(response, OUTPUT);
    }

    @ApiOperation(value = "分销奖励对账报表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = false)
    })
    @RequestMapping(value = "PaymentCheck", method = RequestMethod.GET)
    @L2RPermissionResources(value = paymentCheck, name = "分销奖励报表")
    public void getPaymentCheck(
            HttpServletResponse response,
            @RequestParam(name = "startDate", required = false) String startDate
    ) throws Exception {
        PaymentReportManagerParamDto paymentReportManagerParamDto = new PaymentReportManagerParamDto();
        paymentReportManagerParamDto.setStartDatetime(startDate);
        String OUTPUT = reportFacadeService.getPaymentCheck(paymentReportManagerParamDto);
        outfile(response, OUTPUT);
    }

    @ApiOperation(value = "商城订单发货报表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "expressSta",value = "发货状态", required = false)
    })
    @RequestMapping(value = "MallOrderByExpressSta", method = RequestMethod.GET)
    @L2RPermissionResources(value = mallOrderByExpressSta, name = "商城订单发货报表")
    public void getMallOrderByExpressSta(
            HttpServletResponse response,
            @RequestParam(name = "expressSta", required = false) String expressSta
    ) throws Exception {
        MallOrderManagerParamDto mallOrderManagerParamDto = new MallOrderManagerParamDto();
        mallOrderManagerParamDto.setStas(expressSta);
        String OUTPUT = reportFacadeService.getMallOrderByExpressSta(mallOrderManagerParamDto);
        outfile(response, OUTPUT);

    }

    @ApiOperation(value = "月饼销售概要")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "startDate",value = "开始时间", required = false)
    })
    @RequestMapping(value = "MoonCakeSaleSummary", method = RequestMethod.GET)
    //@L2RPermissionResources(value = mallOrderByExpressSta ,optType = OptType.VIEW)
    public void getMoonCakeSaleSummary(
            HttpServletResponse response,
            @RequestParam(name = "startDate", required = false) String startDate
    ) throws Exception {

        String OUTPUT = reportFacadeService.getMoonCakeSaleSummary(startDate);
        outfile(response, OUTPUT);

    }

    @ApiOperation(value = "月饼销售详情")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "startDate",value = "开始时间", required = false)
    })
    @RequestMapping(value = "MoonCakeSaleDetail", method = RequestMethod.GET)
    //@L2RPermissionResources(value = mallOrderByExpressSta ,optType = OptType.VIEW)
    public void getMoonCakeSaleDetail(
            HttpServletResponse response,
            @RequestParam(name = "startDate", required = false) String startDate
    ) throws Exception {

        String OUTPUT = reportFacadeService.getMoonCakeSaleDetail(startDate);
        outfile(response, OUTPUT);

    }

    @ApiOperation(value = "月饼发货详情")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "startDate",value = "开始时间", required = false)
    })
    @RequestMapping(value = "MoonCakeExpress", method = RequestMethod.GET)
    //@L2RPermissionResources(value = mallOrderByExpressSta ,optType = OptType.VIEW)
    public void getMoonCakeExpress(
            HttpServletResponse response,
            @RequestParam(name = "startDate", required = false) String startDate
    ) throws Exception {
        String OUTPUT = reportFacadeService.getMoonCakeExpress(startDate);
        outfile(response, OUTPUT);

    }

    private void outfile(HttpServletResponse response, String OUTPUT) {
        //1.设置文件ContentType类型，这样设置，会自动判断下载文件类型
        response.setContentType("multipart/form-data");

        //2.设置文件头：最后一个参数是设置下载文件名(假如我们叫a.pdf)
        response.setHeader("Content-Disposition", "attachment;fileName=" + "dynamic_columns_output.xls");
        ServletOutputStream out;
        //通过文件路径获得File对象(假如此路径中有一个download.pdf文件)
        File file = new File(OUTPUT);

        try {
            FileInputStream inputStream = new FileInputStream(file);

            //3.通过response获取ServletOutputStream对象(out)
            out = response.getOutputStream();

            int b = 0;
            byte[] buffer = new byte[inputStream.available()];
            while (b != -1) {
                b = inputStream.read(buffer);
                //4.写到输出流(out)中
                out.write(buffer, 0, b);
            }
            inputStream.close();
            out.close();
            out.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
